﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace MapEditorSDG
{
    class FileHandler
    {
        public static void SaveMap(String _title, GridHandler _grid)
        {
            Stream stream = File.Open(_title, FileMode.OpenOrCreate);
            BinaryFormatter bf = new BinaryFormatter();
            
            //Write the size of the grid at the beginning of the stream. The first length is the column's number and the second one is for the rows.
            stream.Write(BitConverter.GetBytes(_grid.GameCells.GetLength(1)), 0, sizeof(int));
            stream.Write(BitConverter.GetBytes(_grid.GameCells.GetLength(0)), 0, sizeof(int));

            //Serialize the entire grid into one stream to save it in a file.
            for (int i = 0; i < _grid.GameCells.GetLength(1); i++)
                for (int j = 0; j < _grid.GameCells.GetLength(0); j++)
                    bf.Serialize(stream, _grid.GameCells[j, i]);

            stream.Close();
        }

        public static GameCell[,] LoadMap(String _title, GridHandler _grid)
        {
            int row = 0;
            int column = 0;

            Stream stream = File.Open(_title, FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();

            //Retrieve both the number of rows and the number of column at the beginning of the stream.
            byte[] mapSize = new byte[sizeof(int)*2];
            stream.Read(mapSize, 0, sizeof(int)*2);

            row = BitConverter.ToInt32(mapSize, 0);
            column = BitConverter.ToInt32(mapSize, sizeof(int));

            GameCell[,] cells = new GameCell[column, row];

            for(int i = 0; i < row; i++)
                for (int j = 0; j < column; j++)
                {
                    cells[j, i] = (GameCell)bf.Deserialize(stream);
                    cells[j, i].Cell.Parent = _grid;
                }

            stream.Close();

            return cells;
        }

        public static void SaveMapToJSON(String _title, GridHandler _grid)
        {
            if(!System.IO.File.Exists(_title))
            {
                System.IO.StreamWriter file = System.IO.File.CreateText(_title);
                file.Close();
            }

            //Write the path to the image in the file.
            File.WriteAllText(_title, _grid.ImgSrc);

            StreamWriter stream = new StreamWriter(_title, true);
            stream.Write(_grid.ToJSON());

            stream.Close();
        }

        public static GameCell[,] LoadMapFromJSON(string _title, GridHandler _grid)
        {
            String map = (String) File.ReadAllLines(_title).ElementAtOrDefault(0);

            _grid.ImgSrc = map.Substring(0, map.IndexOf('['));

            map = map.Remove(0, _grid.ImgSrc.Length);

            //Retrieve the game cells from the stream.
            _grid.FromJSON(map);
            _grid.ApplyImage();

            return _grid.GameCells;
        }

        public static void NewMap(GridHandler _grid)
        {
            _grid.reset();
        }
    }
}
